﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Admin.SystemSet
{
    public partial class AdminList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindRole();
                LoadContent(false);

                // 从BasePage中得到
                PageLiteral.Text = BuildPageLinks();
            }
        }


        #region 加载列表
        private void LoadContent(bool mark)
        {

            //设置控件属性，从BasePage中得到
            PageConfig(AdminUPager);

            LoadCondition();

            string roleid = ddlRole.SelectedValue;

            //List<Member> list = new List<Member();
            UsersHelper uhelper = new UsersHelper();
            Criteria c = new Criteria();

            if (roleid != "-1")
            {
                c.Add(CriteriaType.Equals, "RoleID", roleid);
            }

            c.Add(CriteriaType.Equals, "RoleType", 0);
            c.Add(CriteriaType.NotEquals, "RoleID", 1);

            AdminUPager.ItemCount = uhelper.QueryUsersCountBySearch(c);

            // 以下的值从BasePage中得到
            AdminUPager.UrlFormat = UrlFormat;
            AdminUPager.PrefixText = GetPreFixText(AdminUPager);

            if (mark)
            {
                PagerInit(AdminUPager);
            }

            string[] fields = new string[] { "ID", "RoleID", "RoleType", "UserName", "NickName", "Flag", "CreateDate" };

            DataTable dt = new DataTable();
            dt = uhelper.QueryUsersBySearchToTable(c, new Order[] { new Order("CreateDate",OrderMode.Desc) }, AdminUPager.Begin - 1, AdminUPager.Count, fields);

            dt.Columns.Add(new DataColumn("RoleName"));
            dt.Columns.Add(new DataColumn("strFlag"));
            dt.Columns.Add(new DataColumn("strCreateDate"));

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    DateTime dtCreateDate = Convert.ToDateTime(dr["createdate"]);
                    dr["strCreateDate"] = dtCreateDate.ToString("yyyy-MM-dd");

                    string getroleid = dr["roleid"] + "";
                    Role rinfo = new Role();
                    RoleHelper rhelper = new RoleHelper();
                    rinfo = rhelper.GetRole(getroleid);

                    dr["rolename"] = rinfo.RoleName;

                    string getflag = dr["flag"] + "";
                    string strflag = "";
                    if (getflag == "0")
                    {
                        strflag = "启用";
                    }
                    else if (getflag == "9")
                    {
                        string lockdate = Convert.ToString(dr["lockdate"] + "");
                        strflag = "禁用" + "(" + lockdate + ")";
                    }
                    dr["strflag"] = strflag;
                }
            }

            rptAdmin.DataSource = dt;
            rptAdmin.DataBind();
        }
        #endregion

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            SetCondition();
            LoadContent(true);
        }

        #region 列表事件
        protected void rptAdmin_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");

            string id = hidID.Value;

            if (e.CommandName == "close")//禁用
            {
                Users uinfo = new Users();
                UsersHelper uhelper = new UsersHelper();

                uinfo = uhelper.GetUsers(id);
                uinfo.ID = id;
                uinfo.Flag = 9;
                uinfo.LockDate = System.DateTime.Now;
                uhelper.EditUsers(uinfo);

                Tools.Alert("账号禁用成功！");
            }
            else if (e.CommandName == "open")//开启
            {
                Users uinfo = new Users();
                UsersHelper uhelper = new UsersHelper();

                uinfo = uhelper.GetUsers(id);
                uinfo.ID = id;
                uinfo.Flag = 0;
                uhelper.EditUsers(uinfo);

                Tools.Alert("账号开启成功！");
            }

            LoadContent(false);
        }
        #endregion

        #region 查询条件设置
        private void SetCondition()
        {
            string roleid = ddlRole.SelectedValue;

            //将查询条件放入Session中
            Session["QueryList"] = roleid + "@" + "AdminList";

        }

        private void LoadCondition()
        {
            string condition = Session["QueryList"] + "";

            string[] conditionAll = condition.Split('@');
            if (conditionAll.Length == 2)
            {
                string page = conditionAll[1];
                if (page == "AdminList")
                {
                    ddlRole.SelectedValue = conditionAll[0];
                }
            }
        }
        #endregion

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminAdd.aspx");
        }

        #region 加载权限
        private void BindRole()
        {
            RoleHelper rhelper = new RoleHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "RoleType", 0);
            c.Add(CriteriaType.NotEquals, "ID", 1);
            List<Role> list = rhelper.QueryRoleBySearch(c, null);

            ddlRole.DataSource = list;
            ddlRole.DataTextField = "RoleName";
            ddlRole.DataValueField = "ID";
            ddlRole.DataBind();

            ListItem li = new ListItem();
            li.Value = "-1";
            li.Text = "-请选择-";
            ddlRole.Items.Insert(0, li);
        }
        #endregion
    }
}